package lambdasinaction.chap5.ex;

import lambdasinaction.chap5.Trader;
import lambdasinaction.chap5.Transaction;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;

import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.joining;

/**
 * Created by gx on 2017/4/13.
 */
public class MyTest {


    @Test//找出2011所有交易 按交易额排序
    public void test1() {
        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brian", "Cambridge");

        List<Transaction> transactions = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950));
        // （1）2011所有交易
        transactions.stream().filter((t) -> t.getYear() == 2011).sorted(comparing(Transaction::getValue)).forEach(System.out::println);


        //（2） 那些交易员在不同城市待过
        transactions.stream().map((t) -> t.getTrader().getCity()).distinct().forEach(System.out::println);
        System.out.println("~~~~~~~~~~~~");
        //（3） 来自剑桥 按姓名排序
        transactions.stream().filter((t) -> t.getTrader().getCity().equals("Cambridge")).map((t) -> t.getTrader().getName()).distinct().sorted(String::compareTo).forEach(System.out::println);
        System.out.println("~~~~~~~~~~~~");
        //（4） 打印交易员名字 按姓名排序
        transactions.stream().map((t) -> t.getTrader().getName()).distinct().sorted(String::compareTo).forEach(System.out::println);
        System.out.println("(4) 方案 2 ");
        String s = transactions.stream().map((t) -> t.getTrader().getName()).distinct().sorted(String::compareTo).collect(joining());
        System.out.println(s);
        //（5） 有没有在米兰工作
        boolean flag = transactions.stream().anyMatch((t) -> t.getTrader().getCity().equals("Milan"));
        System.out.println(flag);

        //（6） 剑桥 所有交易额
        int c = transactions.stream().filter((t) -> t.getTrader().getCity().equals("Cambridge")).map(Transaction::getValue).reduce((a, b) -> a + b).get();
        System.out.println(c);
        //（7）所有交易最大的
        int b = transactions.stream().map(Transaction::getValue).max(Integer::compareTo).get();
        System.out.println(b);

        //（8）所有交易最小的
        int a = transactions.stream().map(Transaction::getValue).min(Integer::compareTo).get();
        System.out.println(a);
    }

    @Test
    public void test2() {
        IntStream evenNumbers = IntStream.rangeClosed(1, 100).filter(n -> n % 2 == 0);
        System.out.println(evenNumbers.count());
    }

    @Test
    public void test3() {
//        int c = 5;
        Stream<int[]> pythagoreanTripiples =
         IntStream.rangeClosed(1, 100).boxed().flatMap(
                a -> IntStream.rangeClosed(1, 100).filter(b -> Math.sqrt(a * a + b * b) % 1 == 0)
                        .mapToObj( b -> new int[]{a, b, (int) Math.sqrt(a * a + b * b)}
                        )
        );
        pythagoreanTripiples.limit(5).forEach(t-> System.out.println(t[0]+"~~~~~"+t[1]+"~~~~~"+t[2]));


        int c = 110;
        Stream<double[]> pythagoreanTripiples2=IntStream.rangeClosed(1,100).boxed().flatMap(a->
                                                IntStream.rangeClosed(a,100).mapToObj(b->new double[]{a,b,Math.sqrt(a*a+b*b)}))
                                                .filter(t->t[2]%1==0);
        pythagoreanTripiples2.limit(10).forEach(t-> System.out.println(t[0]+"~~~~~"+t[1]+"~~~~~"+t[2]));
    }

    @Test
    public void test4() throws IOException {
        long uniqueWords = Files.lines(Paths.get("data1.txt"), Charset.defaultCharset())
                .flatMap(line -> Arrays.stream(line.split(" ")))
                .distinct()
                .count();
        System.out.println(uniqueWords);
        long uniqueWord = 0;
        try (Stream<String> lines = Files.lines(Paths.get("data1.txt"), Charset.defaultCharset())){
            uniqueWord = lines.flatMap(line -> Arrays.stream(line.split(" ")))
                    .distinct()
                    .count();
        }catch (IOException e){
            e.getStackTrace();
        }
        System.out.println(uniqueWord);
    }
    //裴波纳契元祖序列
    @Test
    public void test5() {
        Stream.iterate(new int[]{0,1},t->new int[]{t[1],t[1]+t[0]}).limit(20).forEach(
                t-> System.out.println(t[0]+"~~~~~~~"+t[1])
        );
    }

}
